package com.lanou.horo.zhuizhui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.horo.zhuizhui.adapter.CartoonMessageAdapter;

import com.lanou.horo.zhuizhui.adapter.OtherAdapter;
import com.lanou.horo.zhuizhui.entity.CartoonMessageItems;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.utility.customDialog.CustomProgressDialog;
import com.lanou.horo.zhuizhui.utility.recyclerView.DividerItemDecoration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/24.
 */
public class CartoonMessageActivity extends AppCompatActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView recyclerView;
    private CartoonMessageAdapter adapter;
    private RequestQueue queue;
    private ImageView backBtn;
    private String url;
    private OtherAdapter otherAdapter;

    private SwipeRefreshLayout swipeRefreshWidget;
    private int lastVisibleItem;
    private CustomProgressDialog dialog;
    private boolean isdialogshowing = false;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    swipeRefreshWidget.setRefreshing(true);
                    if (adapter.getArticlesEntities() != null) {
                        adapter.getArticlesEntities().clear();
                        initData();
                    }
                    break;
            }
        }
    };;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_message);
        initView();
        initUrl();
        initData();
    }

    private void initView() {
        swipeRefreshWidget = (SwipeRefreshLayout) findViewById(R.id.ctSwipeRefresh);

        backBtn = (ImageView) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.cartoonMessageRecyclerView);
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        manager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));

        swipeRefreshWidget.setColorSchemeColors(0xff00aa00, 0xfffedcba, 0xff123456, 0xff654321);
        swipeRefreshWidget.setOnRefreshListener(this);
        swipeRefreshWidget.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources()
                        .getDisplayMetrics()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    private void initUrl() {
        dialog = new CustomProgressDialog(this, "加载中...", R.anim.loding);
        dialog.setCanceledOnTouchOutside(false);
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        switch (id) {
            case 56://今日追追资讯
                url = "http://api.zhuizhuiyoyo.com/request.php?method=article%2Flist&timestamp=144584" +
                        "8543882&param=%7B%22count%22%3A20%2C%22page%22%3A1%2C%22user_token%22%3A%22a8d18aee" +
                        "4cbb64ce57e29ef40c8b66d8%22%2C%22user_id%22%3A2036264%7D&sig=5a16f5aed6bb2aae03c8a466f854b95a";
                adapter = new CartoonMessageAdapter(this);
                recyclerView.setAdapter(adapter);

                break;
//            case 18://热门话题
//                url = "http://aizhuizhui.com/article/list_huati?page=1";
//                otherAdapter = new OtherAdapter(this);
//                recyclerView.setAdapter(otherAdapter);
//                initOtherData();
//                break;
//            case 51://安利
//                url = "http://aizhuizhui.com/article/list_anli?page=1";
//                otherAdapter = new OtherAdapter(this);
//                recyclerView.setAdapter(otherAdapter);
//                initOtherData();
//                break;
//            case 53://黑白漫
//                url = "http://aizhuizhui.com/article/list_culture?page=1";
//                otherAdapter = new OtherAdapter(this);
//                recyclerView.setAdapter(otherAdapter);
//                initOtherData();
//                break;
//            case 49://往期词典
//                url = "http://aizhuizhui.com/article/list_cidian?page=1";
//                otherAdapter = new OtherAdapter(this);
//                recyclerView.setAdapter(otherAdapter);
//                initOtherData();
//                break;


            }
        }

//    private void initOtherData() {
//        queue = Volley.newRequestQueue(this);
//
//        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Gson gson = new Gson();
//                OtherItem oi = gson.fromJson(response,OtherItem.class);
//                if (oi != null){
//                    List<OtherItem.ArticlesEntity> entities = oi.getArticles();
//                    if (entities != null && entities.size() > 0){
//                        otherAdapter.addData(entities);
//                    }}
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                error.printStackTrace();
//            }
//        }) {
//            @Override
//            public Map<String, String> getHeaders()
//                    throws AuthFailureError {
//                HashMap<String, String> map = new HashMap<String, String>();
//                map.put("Accept-Encoding", "");
//                return map;
//            }
//        };
//        queue.add(request);
//    }
    private void initData() {

        if (isdialogshowing == false) {
            dialog.show();
            isdialogshowing = true;
        }
        queue = Volley.newRequestQueue(this);

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                CartoonMessageItems cmis = gson.fromJson(response, CartoonMessageItems.class);
                if (cmis != null) {
                    List<CartoonMessageItems.ArticlesEntity> list = cmis.getArticles();
                    if (list != null && list.size() > 0) {
                        adapter.addArtList(list);
                    }
                }
                swipeRefreshWidget.setRefreshing(false);
                if (isdialogshowing == true) {
                    dialog.dismiss();
                    isdialogshowing = false;}
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        queue.add(request);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backBtn:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        if (isdialogshowing == false) {
            dialog.show();
            isdialogshowing = true;
        }
        handler.sendEmptyMessageDelayed(0, 3000);

    }
}
